package com.yixing.tech.wms.service.impl;

import com.alibaba.fastjson.JSON;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.CommonConstant;
import com.yixing.tech.common.service.AbstractWmsService;
import com.yixing.tech.wms.entity.BaseSyncEntity;
import com.yixing.tech.wms.entity.ErpWareHouse;
import com.yixing.tech.wms.entity.Material;
import com.yixing.tech.wms.service.ErpSyncService;
import com.yixing.tech.wms.utils.DocumentUtils;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.yixing.tech.common.CommonConstant.ERP_WAREHOUSE;
import static com.yixing.tech.common.CommonConstant.MATERIAL;

/**
 * ContainerService
 *
 * @author yixing tech
 * @since 1.0.0
 */
@Slf4j
@Service
public class ErpSyncServiceImpl extends AbstractWmsService implements ErpSyncService {

    @Value("${tenant-id: 6204cb763da841718ddd1cf8}")
    private String tenantId;

    @Override
    public boolean insertWarehouse(ErpWareHouse erpWareHouse) throws BusinessException {
        // 查重
        Document document = simpleFindOne("entity_" + ERP_WAREHOUSE, "code", erpWareHouse.getCode());
        if(document != null){
            throw new BusinessException("仓库编码重复");
        }
        erpWareHouse.setTenantId(tenantId);
        mongoTemplate.save(erpWareHouse, "entity_" + ERP_WAREHOUSE);
        return true;
    }

    @Override
    public boolean updateWarehouse(ErpWareHouse erpWareHouse) throws BusinessException {
        // 查询是否存在
        Document document = simpleFindOne("entity_" + ERP_WAREHOUSE, "code", erpWareHouse.getCode());
        if(document == null){
            throw new BusinessException(String.format("仓库[%s]不存在", erpWareHouse.getCode()));
        }
        return updateById(document, DocumentUtils.genUpdate(erpWareHouse), "entity_" + ERP_WAREHOUSE);
    }

    @Override
    public boolean syncWarehouse(ErpWareHouse erpWareHouse) throws BusinessException {
        return syncEntity(erpWareHouse, ERP_WAREHOUSE);
    }

    @Override
    public boolean insertMaterial(Material material) throws BusinessException {
        // 查重
        Document document = simpleFindOne("entity_" + MATERIAL, "code", material.getCode());
        if(document != null){
            throw new BusinessException("物料编码重复");
        }
        material.setTenantId(tenantId);
        mongoTemplate.save(material, "entity_" + MATERIAL);
        return true;
    }

    @Override
    public boolean updateMaterial(Material material) throws BusinessException {
        // 查询是否存在
        Document document = simpleFindOne("entity_" + MATERIAL, "code", material.getCode());
        if(document == null){
            throw new BusinessException(String.format("物料[%s]不存在", material.getCode()));
        }
        return updateById(document, DocumentUtils.genUpdate(material), "entity_" + MATERIAL);
    }

    @Override
    public boolean syncMaterial(Material material) throws BusinessException {
        // 查询是否存在
        Document logDoc = new Document();
        logDoc.put("路径", "/open/erp/sync/material");
        logDoc.put("createTime", new Date());
        logDoc.put("createName", "ERP");
        logDoc.put("tenantId", tenantId);
        logDoc.put("IP地址", getIp());
        logDoc.put("请求类型", "POST");
        logDoc.put("类型", "三方");
        logDoc.put("参数", JSON.toJSONString(material));
        boolean re = false;
        try {
            re = syncEntity(material, MATERIAL);
            logDoc.put("结果", "true");
        } catch (Exception e) {
            logDoc.put("结果", String.format("异常: %s", e.getMessage()));
        } finally {
            mongoTemplate.insert(logDoc, "entity_" + CommonConstant.LOG_TABLE);
        }
        return re;
    }

    private boolean syncEntity(BaseSyncEntity entity, String objName) throws BusinessException {
        Document document = simpleFindOne("entity_" + objName, "code", entity.getCode());
        if(document == null){
            // 进行新增
            entity.setTenantId(tenantId);
            mongoTemplate.save(entity, "entity_" + objName);
            return true;
        } else {
            Long syncTimeStamp = document.getLong("syncTimeStamp");
            if(syncTimeStamp == null || syncTimeStamp < entity.getSyncTimeStamp()){
                return updateById(document, DocumentUtils.genUpdate(entity), "entity_" + objName);
            } else {
                throw new BusinessException("更新时间不能小于最近的更新时间");
            }
        }
    }
}
